/**
 * Created by Janpoem on 2016/2/1 0001.
 */

const db = require('./db');

const Cache = require('./cache');

const cipher = require('./cipher');

module.exports = new Cache('sessionCache', {
	life: 60 * 10, // 用户身份验证，只缓存10分钟
	onload: function(token, fn) {
		var me = this;
		var realToken = cipher.decrypt(token);
		if (realToken === false || realToken.length <= 0) {
			Cache.call(fn, me, [token, false]);
		}
		else {
			db.query('select id, login_id from user where ws_token = ? limit 1', [realToken], function(err, rows) {
				var data = db.row(rows, 0);
				if (data !== false) {
					me.save(token, data);
				}
				Cache.call(fn, me, [token, data, true]);
			});
		}
	}
});